﻿using Microsoft.Kinect;
using NuiDicomPlayer.Subviews;
//using NuiUserController.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Biomedica.Nui.ui;
using NuiDicomPlayer.Controls;
using NuiDicomPlayer.Controls.Elements;
using NuiDicomPlayer.VirtualMouse;
using Biomedica.Graphics;
using System.Threading;
using KinectLibrary;
using NuiDicomPlayer.Nui.NavigationEngine;

namespace NuiDicomPlayer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region cross threading
        private delegate void LogEventDelegate(String e);
        private delegate void ShowSpeechMessage(String msg);
        private delegate void ShutDownApplication();
        private delegate void ShowHideShadow(bool b);
        private delegate void DoNavigation(NuiNavigator.SkeletonsStateFlags flags);
        #endregion

        
       
        public MainWindow()
        {
            InitializeComponent();
            vtkplayer.OnQuitBtnPressed += vtkplayer_OnQuitBtnPressed;
            vtkplayer.ShowPlayer(false);
        }
        #region UI Callbacks
        void vtkplayer_OnQuitBtnPressed(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(new ShutDownApplication(CloseApplication));
        }
       
        void CloseApplication()
        {
            vtkplayer.ClosePipeline();
            vtkplayer.ShowPlayer(false);
            
            App.Nav.StopGestureAndPostureDetectionCycle();
            
            App._Manager.EnableKinect = false;

            Environment.Exit(0);
           
        }

        private void OnScreen1Loaded(object sender, RoutedEventArgs e)
        {
           
            #region Sensor initalising
            App.StartSensor();
            App.ActiveMouse = this.rightmousepointer;
            App.ActiveMouse = rightmousepointer;
            App.ActiveWindow = welcom_screen;
            #endregion
            
         
            welcom_screen.StartDistance = this.ActualWidth*2;
            welcom_screen.Width = this.ActualWidth;
            welcom_screen.Height = this.ActualHeight;
            welcom_screen.ShowView(); 
           
            vtkplayer.LoagEvent("MainWIndow Loaded");
            vtkplayer.OnQuitBtnPressed+=vtkplayer_OnQuitBtnPressed;

            #region Navigation Thread
            App.Nav = NuiNavigator.New();
            App.Nav.OnTriggerAction += ResponsToTriggerAction;
            
            #endregion
        }

        #region Perform State Diagram Transtions
        private void ResponsToTriggerAction(NuiNavigator.SkeletonsStateFlags flags)
        {
            this.Dispatcher.Invoke(new DoNavigation(doNavigations), flags);
        }
        private void doNavigations(NuiNavigator.SkeletonsStateFlags flags)
        {
           NavigationCommand(flags);
          
            
        }
        private void NavigationCommand(NuiNavigator.SkeletonsStateFlags flags)
        {
            if (flags.IsSkeletonInStage) SkeletonInstage(flags);
            else SkeletonOutofStage();
            vtkplayer.DoNavigateCommand(flags);

        }
       
        #endregion

        #region Detector
        void Nav_OnBothHandsNotINSpaceRoutedEvent(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(new LogEventDelegate(LogEvent), "both hands not in space");
        }

        void Nav_OnBothHandsINSpaceRoutedEvent(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(new LogEventDelegate(LogEvent), "both hands  in space");
        }
        void Nav_OnSkeletonNotStableRoutedEvent(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(new LogEventDelegate(LogEvent), "skeleton Is not stable");
        }
        void Nav_OnSkeletonStableRoutedEvent(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(new LogEventDelegate(LogEvent), "skeleton Is stable");
        }
        void Nav_OnSkeletonOutstageRoutedEvent(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(new LogEventDelegate(LogEvent), "skeleton Is out of instage");
        }
        void Nav_OnSkeletonInstageRoutedEvent(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(new LogEventDelegate(LogEvent), "skeleton Is in instage");
        }
         void Nav_OnRightHandIsNotStable(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(new LogEventDelegate(LogEvent), "Righthand is not stable");
        }

        void Nav_OnRightHandIsStable(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(new LogEventDelegate(LogEvent),"Righthand is stable");
        }

        void Nav_OnLeftHandIsNotStable(object sender, EventArgs e)
        {
           
            this.Dispatcher.Invoke(new LogEventDelegate(LogEvent), "Lefthand is not stable");
        }

        void Nav_OnLeftHandIsStable(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(new LogEventDelegate(LogEvent), "Lefthand is  stable");
        }


       
         #endregion
        private void LogEvent(string p)
        {
            vtkplayer.LoagEvent(p);
        }
        void dicomplayerContainer_OnQuitBtnPressed(object sender, EventArgs e)
        {
            CloseApplication();
        }
        private void WordRecognised(string action, string Message)
        {
            switch (action)
            {
                case ("speech_recognised"):
                    this.Dispatcher.Invoke(new ShowSpeechMessage(PerformAction), Message);
                    break;
            }
        }
        public void PerformAction(String Message)
        {
            switch (Message)
            {
                case ("kinect quit"):
                    CloseApplication();
                    break;               
                case("kinect load new file"):
                    vtkplayer.LoadNewDicomFile();
                    break;
                case ("kinect show record informations"):
                    vtkplayer.ShowPataintInformation();
                    break;
                case ("kinect close"):
                    App.ActiveWindow.TriggerClose();
                    break;
                case ("kinect play slice"):
                    vtkplayer.PlaySlice(true);
                    break;
                case ("kinect play slice in reverse"):
                    vtkplayer.PlaySlice(false);
                    break;
                case ("kinect pause slice"):
                    vtkplayer.PausePlaying();
                    break;
                case ("kinect show sagital"):
                    vtkplayer.Switch2Axial();
                    break;
                case ("kinect show coronal"):
                    vtkplayer.Switch2Coronal();
                    break;
                case ("kinect show axial"):
                    vtkplayer.Switch2Sagital();
                    break;
                case ("kinect reset brightness"):
                    vtkplayer.RestBrightness();
                    break;
                case ("kinect reset contrast"):
                    vtkplayer.ResetContrast();
                    break;
                case ("kinect zoom all"):
                    vtkplayer.ZoomAll();
                    break;
                
            }
            vtkplayer.VoiceCommandText = Message;
        }
        private void welcom_screen_OnViewDisappear_1(object sender, EventArgs e)
        {
            App.ActiveWindow = vtkplayer;
            vtkplayer.ShowPlayer(false);
        }
        private void SkeletonInstage(NuiNavigator.SkeletonsStateFlags flags)
        {
            App.ActiveMouse.Enabled = true;
            App._Manager.Tracking = true;
            vtkplayer.VoiceCommandText = "hand in stage";
            if (flags.IsBoothHandInSpace)
            {
                vtkplayer.VoiceCommandText = "hand in space";
                if (flags.IsRightHandStable) vtkplayer.VoiceCommandText = "right hand is stabel";
               
            }

        }
        private void SkeletonOutofStage()
        {
          //  App.ActiveMouse.Enabled = false;
            App._Manager.Tracking = false;
            vtkplayer.VoiceCommandText = "hand in oute of stage";
        }
      
        internal void OnDepthFrameReady(WriteableBitmap bmp)
        {

            this.Dispatcher.Invoke(new DepthFrameReady(XthreadDisplayFrame), bmp);
             
        }
        internal void XthreadDisplayFrame(WriteableBitmap Bmp)
        {
            welcom_screen.ShowShadow(Bmp);
        }
        #endregion
    }
}
